from datetime import datetime
from Common import sql_content

class Job_place(object):
    def __init__(self):
        self.count = 0
        self.sql_content = sql_content.sql_content()
        self.area_all = []
        # self.country = {'北京':'beijing','上海':'sahnghai' ,'天津':'tianjin','重庆':'chongqing','黑龙江':'heilongjiang', '吉林':'jilin',
        #                   '辽宁':'liaoning', '内蒙古':'neimenggu','河北':'hebei' ,'新疆':'xinjiang', '甘肃':'gansu',
        #                   '青海':'qinghai','陕西': 'shanxi3' ,'宁夏':'ningxia', '河南':'henan' ,'山东':'shandong','山西':'shanxi1',
        #                   '安徽':'anhui', '湖南':'hunan','湖北':'hubei' ,'江苏':'jiangsu','四川':'sichuan' ,'贵州':'guizhou',
        #                   '云南':'yunnan', '广西':'guangxi','西藏' :'xizang' ,'浙江':'zhejiang', '江西':'jiangxi' ,'广东':'guangdong' ,
        #                 '哈尔滨':'haerbin','长春':'changchun','沈阳':'shenyang','呼和浩特':'huhehaote','石家庄':'shijiazhuang',
        #                 '乌鲁木齐':'wulumuqi','兰州':'lanzhou','西宁':'xining','西安':'xian','银川':'yinchuan','郑州':'zhengzhou',
        #                 '济南':'jinan','太原':'taiyuan','合肥':'hefei','长沙':'changsha','武汉':'wuhan','南京':'nanjing','成都':'chengdu',
        #                 '贵阳':'guizhou','昆明':'kunming','南宁':'nanning','拉萨':'lasa','杭州':'hangzhou','南昌':'nanchang',
        #                 '广州':'guangzhou'
        # }
        self.country = {'北京':'北京','上海':'上海' ,'天津':'天津','重庆':'重庆','黑龙江':'哈尔滨', '吉林':'长春',
                          '辽宁':'沈阳', '内蒙古':'呼和浩特','河北':'石家庄' ,'新疆':'乌鲁木齐', '甘肃':'兰州',
                          '青海':'西宁','陕西': '西安' ,'宁夏':'银川', '河南':'郑州' ,'山东':'济南','山西':'太原',
                          '安徽':'合肥', '湖南':'长沙','湖北':'武汉' ,'江苏':'南京','四川':'成都' ,'贵州':'贵阳',
                          '云南':'昆明', '广西':'南宁','西藏' :'拉萨' ,'浙江':'杭州', '江西':'南昌' ,'广东':'广州' ,
        }
        self.country_count = {
            'beijing', 'shanghai','tianjin','chongqing', 'heilongjiang', 'haerbin', 'jilin' ,'changchun',
            'liaoning','shenyang', 'neimenggu','huhehaote', '河北', '石家庄', '新疆', '乌鲁木齐', '甘肃','兰州',
            '青海', '西宁', '陕西', '西安', '宁夏', '银川', '河南', '郑州', '山东', '济南', '山西', '太原',
            '安徽', '合肥', '湖南', '长沙', '湖北', '武汉', '江苏', '南京', '四川', '成都', '贵州', '贵阳',
            '云南', '昆明', '广西', '南宁', '西藏', '拉萨', '浙江', '杭州', '江西', '南昌', '广东', '广州',
            '福建', '福州', '台湾', '台北', '海南', '海口', '香港', '香港', '澳门', '澳门'
        }
        self.area_count_dict = {}
        self.atea_count_dict_new = {'other':0}
        self.this_dict = {}
# 小米社招Job_place
    def select_area_she(self):
        con = self.sql_content.conten()
        cursor = con.cursor()
        sql_xiaomi_she = "select Job_Place from data_xiaomi_she"

        cursor.execute(sql_xiaomi_she)
        rs = cursor.fetchall()
        for row in rs:
            #print("Job_Place=%s" % row)#%---ceshi---%
            self.area_all.append(row[0])
        #print(cursor.rowcount)

        cursor.close()
        con.close()

# 小米校招Job_place
    def select_area(self):
        con = self.sql_content.conten()
        sql_xiaomi = "select Job_Place from data_xiaomi"
        cursor = con.cursor()
        cursor.execute(sql_xiaomi)
        rs_1 = cursor.fetchall()
        for row_1 in rs_1:
            #print("hhhhh=%s" % row_1)  # %---ceshi---%
            self.area_all.append(row_1[0])
        cursor.close()
        con.close()
#51社招Job_Place
    def select_area_51job(self):
        con = self.sql_content.conten()
        sql_51job = "select Job_Place from data_51job_she"
        cursor = con.cursor()
        cursor.execute(sql_51job)
        rs = cursor.fetchall()
        for row in rs:
            #print("hhhhh=%s" % row)  # %---ceshi---%
            self.area_all.append(row[0])
        cursor.close()
        con.close()


    def select_main(self):
        self.select_area_she()
        self.select_area()
        self.select_area_51job()
        #(self.area_all)# %---ceshi---%
        #print(len(self.area_all))# %---ceshi---%
        #print('这是mantoo啊' ,set(self.area_all) , len(set(self.area_all)))
        area_set = (set(self.area_all))
        for area in area_set:
            area_count = self.area_all.count(area)
            self.area_count_dict[area] = area_count
        #print(self.area_count_dict)
        # hhh = 0
        # for v in self.area_count_dict:
        #     hhh += self.area_count_dict[v]
        # print(hhh)

        for one in self.country:
            count_country = 0
            for two in self.area_count_dict:
                if(two.count(self.country[one]) > 0):
                    count_country += self.area_count_dict[two]

            if(count_country != 0):
                self.atea_count_dict_new[self.country[one]] = count_country
            if (count_country == 0):
                #self.atea_count_dict_new['other'] += 1000
                pass
        # print('看可以' ,self.atea_count_dict_new)
        # hhh = 0
        # for v in self.atea_count_dict_new:
        #     hhh += self.atea_count_dict_new[v]
        # print(hhh)

        num_dict = []
        for k in self.atea_count_dict_new:
            num_dict.append(self.atea_count_dict_new[k])
        #print(num_dict)
        num_dict.sort(reverse=True)
        fill_count = 0
        for s in range(10,len(self.atea_count_dict_new)):
            fill_count += num_dict[s]
        print(num_dict)
        num_dict = num_dict[0:10]
        print(num_dict)
        num_dict.append(fill_count)
        for x in self.atea_count_dict_new:
            for l in num_dict:
                if(self.atea_count_dict_new[x] == l):
                    self.this_dict[x] =l
        #self.this_dict['other'] = num_dict[11]
        #print(self.atea_count_dict_new)
        print(self.this_dict)
        #print(num_dict)

        return self.this_dict

    def name_main(self):
        page = {}
        page['title'] = '就业岗位最多的前10个城市'
        page['int'] = """
                    Praesent mattis mauris urna. Suspendisse vulputate, urna sit amet laoreet iaculis, magna urna porttitor justo, vel pretium sapien turpis nec turpis. Integer nec consectetur odio. Mauris quis neque velit. Aenean nec metus leo, non pellentesque ero. Ut et elit nibh, sit amet rutrum leo. Nulla aliquet, sem vel faucibus tristique, mi erat gravida odio, et feugiat ipsum.

            Sed mi urna, adipiscing eget dignissim ac, dictum in nisl. Fusce id erat orci, ut placerat augue. Nulla tortor ligula, feugiat tincidunt accumsan ac, feugiat eget mi. Fusce euismod elit eget erat viverra pharetra. Aliquam tellus purus, convallis in tempor at, lacinia non lectus. Vivamus aliquet dictum volutpat. Sed sit amet urna tellus. Mauris id lacus arcu, nec volutpat odio. Sed risus urna, pulvinar vitae fermentum at, rutrum vitae arcu. Proin ante orci, congue quis molestie ac, ornare ac ipsum. Proin sit amet semper orci. Donec posuere, mi et tempus adipiscing, elit odio auctor eros, in fringilla quam velit sed dolor. Duis aliquam tristique nulla.
                """
        page['type'] = '毕业生,就业岗位, 10大城市, 图标, 饼状图'
        page['unknow'] = 'view'
        page['date'] = '2017/8/27'
        page['list'] = {'小米招聘': 'www', '华为招聘': 'www', '阿里招聘': 'www', '百度招聘': 'www', '51招聘': 'www'}

        if(self.count != 0):
            pass
        elif(self.count == 0 or datetime.now().strftime('%H:%M') == '00:00'):
            area_main = Job_place()
            all_area_count = area_main.select_main()
            self.count += 1

        return all_area_count,page

if __name__ == "__main__":
    area_main = Job_place()
    area_main.select_main()

    #print(time.strftime('%Y-%m-%d', time.loadtime(time.time())))